package com.example.duanlei417.mypro2.login.app;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.example.duanlei417.mypro2.login.LoginActivity;
import com.example.duanlei417.mypro2.login.lib_login.core.ILogin;
import com.example.duanlei417.mypro2.login.lib_login.core.LoginSDK;
import com.example.duanlei417.mypro2.login.utils.SharePreferenceUtil;

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        LoginSDK.getInstance().init(this, mILogin);
    }

    ILogin mILogin = new ILogin() {

        /**
         * 处理登录
         * @param applicationContext
         * @param userDefine  分辨不同的登录处理方式
         */
        @Override
        public void login(Context applicationContext, int userDefine) {
            switch (userDefine) {
                case 0: //跳转到登录页
                    Intent intent = new Intent(applicationContext, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

                    break;
                case 1: //只给提示
                    Toast.makeText(applicationContext, "您还没有登录，请登录后执行",
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(applicationContext, "您还没有登录，请登录后执行",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }

        @Override
        public boolean isLogin(Context applicationContext) {
            return SharePreferenceUtil.getBooleanSp(SharePreferenceUtil.IS_LOGIN, applicationContext);
        }

        @Override
        public void clearLoginStatus(Context applicationContext) {
            SharePreferenceUtil.setBooleanSp(SharePreferenceUtil.IS_LOGIN, false,
                    applicationContext);
        }
    };
}
